package com.zrz.service;

import com.zrz.dao.auto.RedcodeMapper;
import com.zrz.dao.auto.UserMapper;
import com.zrz.model.auto.Redcode;
import com.zrz.model.auto.RedcodeExample;
import com.zrz.model.auto.User;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MineService {
    @Autowired
    private RedcodeMapper redcodeMapper;
    @Autowired
    private UserMapper userMapper;
    public Map vipByCardNum(User user, String code){
        Map map=new HashMap();
        RedcodeExample redcodeExample=new RedcodeExample();
        redcodeExample.createCriteria().andRedcodeEqualTo(code).andStatusEqualTo(false);
        List<Redcode> redcodeList=redcodeMapper.selectByExample(redcodeExample);
        if(CollectionUtils.isEmpty(redcodeList)){
            map.put("code",-1);
            map.put("msg","兑换码不存在");
        }else{
            Redcode redcode=redcodeList.get(0);
            //更新操作
            redcode.setUid(user.getId());
            redcode.setStatus(true);
            redcodeMapper.updateByPrimaryKeySelective(redcode);
            //更新激活会员
            user.setMember(true);
            user.setSurplusCount(36);
            userMapper.updateByPrimaryKeySelective(user);
            map.put("code",1);
            map.put("msg","激活会员成功");
        }
        return map;
    }
}
